package com.ztesoft.report.controller;


import com.alibaba.fastjson.JSON;
import com.ztesoft.report.base.controller.BaseController;
import com.ztesoft.report.base.pojo.R;
import com.ztesoft.report.dao.SysDictDao;
import com.ztesoft.report.dao.SysSqlDao;
import com.ztesoft.report.entity.ReportData;
import com.ztesoft.report.entity.SysDict;
import com.ztesoft.report.entity.SysSql;
import com.ztesoft.report.entity.dto.ReportDataDto;
import com.ztesoft.report.service.ReportDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author y3tu
 * @since 2018-06-15
 */
@RestController
@RequestMapping("/reportdata")
public class ReportDataController extends BaseController {

    @Autowired
    private ReportDataService reportDataService;

    @Autowired
    private SysDictDao sysDictDao;
    @Autowired
    private SysSqlDao sysSqlDao;

    /**
     * 添加
     * @param reportDataDto
     * @return
     */
    @RequestMapping("/add")
    public R add(@RequestBody ReportDataDto reportDataDto){

        ReportData reportData=new ReportData();
        reportData.setCreateTime(new Date());
        reportData.setCreateUser(getCurrentUser());
        reportData.setUpdateTime(new Date());
        reportData.setUpdateUser(getCurrentUser());
        BeanUtils.copyProperties(reportDataDto, reportData);
        reportData.setSqls(JSON.toJSONString(reportDataDto.getSqls()));
        reportData.setParams(JSON.toJSONString(reportDataDto.getParams()));
        boolean i=reportDataService.insert(reportData);
        if (i==true)
            return R.ok("操作成功", reportData.getId());
        else
            return R.error("操作失败") ;

    }

    /**
     * 编辑
     * @param reportDataDto
     * @return
     */
    @RequestMapping("/edit")
    public R edit(@RequestBody ReportDataDto reportDataDto){


        ReportData reportData=new ReportData();
        reportData.setUpdateTime(new Date());
        reportData.setUpdateUser(getCurrentUser());
        BeanUtils.copyProperties(reportDataDto, reportData);
        reportData.setSqls(JSON.toJSONString(reportDataDto.getSqls()));
        reportData.setParams(JSON.toJSONString(reportDataDto.getParams()));
        boolean i=reportDataService.updateById(reportData);
        if (i==true) {
            return R.ok("操作成功", reportData.getId());
        }
        else{
            return R.error("操作失败");
        }

    }

    /**
     * 删除
     * @param reportData
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody ReportData reportData){
        boolean i=reportDataService.deleteById(reportData);
        if (i==true)
            return R.ok("操作成功", reportData.getId());
        else
            return R.error("操作失败") ;
    }

    /**
     * 查询数据源表中所有配置的数据源信息
     * @return
     */
    @RequestMapping("/list")
    public Map<String, Object> datasourceList(@RequestBody Map<String, Object> map){

        Map result = null;
        try{

            result = reportDataService.queryPage1(map);
            System.out.println(result);
        } catch (Exception e) {
            System.out.println(e);
            result = R.error("查询失败,服务器异常，请联系相关人员！");
        }

        return result;

    }


    @RequestMapping("/sqlanddict")
    public Map<String, Object> sysDictAndCode(){
        // List<SysDict> sysDictList=sysDictService.sel  sysSqlDao
        Map<String, Object> map=new HashMap<>();
        List<SysDict> mapList=sysDictDao.queryPage(null);
        List<SysSql> mapList2=sysSqlDao.queryPage(null);
        map.put("sysdicts",mapList);
        map.put("syssqls",mapList2);
        return map;
    }


    @RequestMapping("/dict")
    public List<Map<String, Object>>  sysDict(@RequestBody Map<String, Object> map){
        // List<SysDict> sysDictList=sysDictService.sel  sysSqlDao
        if (map.get("type").equals("dict")){
            List<Map<String, Object>> mapList=sysDictDao.sysDictList(map);
            return mapList;
        }else{
            List<Map<String, Object>> mapList2=sysSqlDao.getListBySqlCode(map);
            return  mapList2;
        }

    }

    @RequestMapping("/totalRun")
    public R totalRun(@RequestBody Integer id) {
        ReportData reportData = reportDataService.selectById(id);
        return reportDataService.totalRun(reportData);
    }
}
